package com.sjh.lib_app

import android.content.Context
import android.view.Gravity
import android.widget.LinearLayout
import pl.droidsonroids.gif.GifDrawable
import com.scwang.smartrefresh.layout.api.RefreshHeader
import com.scwang.smartrefresh.layout.api.RefreshInternal
import com.scwang.smartrefresh.layout.api.RefreshKernel
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.constant.RefreshState
import com.scwang.smartrefresh.layout.constant.SpinnerStyle
import com.sjh.baseutils.dp

import pl.droidsonroids.gif.GifImageView

/**
 * explain : 这是下拉刷新控件的头部刷新动画
 * author : SunJH
 * data : 2020/4/8
 */
open class MyRefreshHead(context: Context) : LinearLayout(context),
    RefreshInternal,
    RefreshHeader {
    var drawable: GifDrawable? = null
    val showViw by lazy {
//        ImageView(context)
        GifImageView(context)
    }

    override fun getSpinnerStyle() = SpinnerStyle.Translate

    init {
        showViw.setImageResource(R.drawable.app_good_ref)
        drawable = showViw.drawable as GifDrawable
        drawable?.stop()
        showViw.layoutParams = LayoutParams(33.dp.toInt(), 23.dp.toInt())
        this.addView(showViw)
    }

    override fun onFinish(refreshLayout: RefreshLayout, success: Boolean): Int {
        drawable?.stop()
        return 500
    }

    override fun onInitialized(kernel: RefreshKernel, height: Int, maxDragHeight: Int) {
        this.layoutParams = LayoutParams(-1, 80.dp.toInt())
        this.gravity = Gravity.CENTER
    }

    override fun onHorizontalDrag(percentX: Float, offsetX: Int, offsetMax: Int) {}
    override fun onReleased(refreshLayout: RefreshLayout, height: Int, maxDragHeight: Int) {}
    override fun getView() = this
    override fun setPrimaryColors(vararg colors: Int) {}
    override fun onStartAnimator(refreshLayout: RefreshLayout, height: Int, maxDragHeight: Int) {}

    override fun onStateChanged(refreshLayout: RefreshLayout, oldState: RefreshState, newState: RefreshState) {}

    override fun onMoving(isDragging: Boolean, percent: Float, offset: Int, height: Int, maxDragHeight: Int) {
        drawable?.start()
    }

    override fun isSupportHorizontalDrag() = false

}
